import { config } from '../../config/index';
import { getSelectedSchoolId } from '../../userData/userData'
/** 获取商品列表 */
function mockFetchGoodsList(pageIndex = 1, pageSize = 20) {
  const { delay } = require('../_utils/delay');
  const { getGoodsList } = require('../../model/goods');
  return delay().then(() =>
    getGoodsList(pageIndex, pageSize).map((item) => {
      return {
        spuId: item.spuId,
        thumb: item.primaryImage,
        title: item.title,
        price: item.minSalePrice,
        originPrice: item.maxLinePrice,
        tags: item.spuTagList.map((tag) => tag.title),
      };
    }),
  );
}

/** 获取商品列表 */
export function fetchGoodsList(pageIndex = 1, pageSize = 20) {
  // if (config.useMock) {
  // return mockFetchGoodsList(pageIndex, pageSize);
  // }
  return new Promise((resolve) => {
    // resolve('real api');
    wx.cloud.callFunction({
      name: "good_getGoodListforInstitution",
      data: {
        institution_id: getSelectedSchoolId()//"e54110dd644a5301002b870a50f78567"
      }
    }).then(res => {
      var goods = res.result;
      const app = getApp();
      app.globalData.allGoods = goods;
      resolve(goods);
      // for (var i = 0; i < res.result.length; i++) {
      //   console.log(goods[i]); //商品
      //   wx.cloud.callFunction({
      //     name: "good_getParameterforGood",
      //     data: {
      //       good_id: res.result[i]._id
      //     }
      //   }).then(res => {
      //     console.log(res.result);//商品参数
      //   })
      // }
    })
  });
}
